(function(Layout){
	//form流布局
	Ccay.ViewLayout.add("form-Flow",{
		partStart : '<div class="${css}"><ul>',
		layout :'<li class="layout-item ccay-form-row ccay-form-${item.layout}" field="${item.field}"><samp class="layout-title ${item.titleMark}">${item.name}</samp><span class="ccay-form-input">${item}</span></li>',
		partEnd : '</ul></div>',
		setPartOp:function(item){
			var isCustom = item.layout == "custom";
			//判断是否为自适应区域
			if(isCustom)
			{
				css = "ccay-form-custom";
				(this.itemCount == 0) && (css += " ccay-form-custommore");
			}
			else
			{
				css = "ccay-form-body";
				this.itemCount > 0  && (css += " ccay-form-bodymore");
			}
			this._prevLayout = item.layout;
			return {css : css};
		},
		setItemOp:function(item){
			//处理必填的样式
			if(this.renderType && this.renderType !== 'view')
			{
				Ccay.Modal.isRequiredField(item) && (item.titleMark = 'required');
			}
			return item;
		},
		checkPartEnd:function(item){
			item.titleMark && delete item.titleMark;
		},
		checkPart:function(item){
			if(this.itemCount)
			{
				//判断当前或者上一个是否为自适应区域，进行闭合操作
				if((item.layout == "custom") != (this._prevLayout == "custom"))
				{
					this.endPart();
					this.beginPart(item);
				}
			}
			else
				this.beginPart(item);
		}
	});
	
	//form table布局（一行显示两个field）
	Layout.add("form-Table",{
		partStart : '<tr>',
		layout :'<th width="20%" class="${item.titleMark}">${item.name}</th><td width="30%" class="" ${item._itemColspan}>${item}</td>',
		partEnd : '</tr>',
		setItemOp:function(item){
			//处理必填的样式
			if(this.renderType && this.renderType !== 'view')
			{
				Ccay.Modal.isRequiredField(item) && (item.titleMark = 'required');
			}
			//$.inArray(item.layout,["custom","whole","range"]);
			//自定义模式
			if(item.layout)
			{
				item._itemColspan = "colspan='3'";
				this._prevItem = null;
			}
			else
			{
				//创建布局变量
				this._prevItem = this.itemCount;
				this._colspanSet[this._prevItem] = 1;
				item._itemColspan = "${^" + this._prevItem + "}";
			}
			return item;
		},
		checkPartEnd:function(item){
			item.titleMark && delete item.titleMark;
			delete item._itemColspan;
		},
		checkPart:function(item){
			//判断计算的个数，进行闭合处理
			if(this._colspanNum == 0)
			{
				this.itemCount > 0 && this.endPart(item);
				this.beginPart(item);
				item.layout || this._colspanNum++;
			}
			else 
			{
				if(item.layout)
				{
					//如果上一个没有闭合，这设置colspan
					if(this._prevItem)
						this._colspanSet[this._prevItem] = 3;
					this.endPart(item);
					this.beginPart(item);
				}
				else
					this._colspanNum = 0;
			}
		},
		endPart:function(item){
			this.proxy(this.base.endPart,item);
			this._colspanNum = 0;
		},
		start:function(){
			this._colspanNum = 0;
			this._prevItem = null;
			this._colspanSet = {};
			return "<table class='ccay-table'>";
		},
		end:function(){
			//判断最后一个是否需要colspan
			if(this._colspanNum && this._prevItem)
				this._colspanSet[this._prevItem] = 3;
			
			delete this._colspanNum;
			delete this._prevItem;
			return this.proxy(this.base.end) + "</table>";
		},
		//处理预留下来的变量
		replaceVar:function(tmpl){
			if(!$.isEmptyObject(this._colspanSet))
			{
				var colspan,layout = this;
				return tmpl.replace(/\${\^([\s\S]*?)}/g,function(item,matched){
					colspan = layout._colspanSet[matched];
					if(colspan && colspan > 1)
	            		return "colspan='" + colspan + "'";
	            	return "";
	        	});
			}
			return tmpl;
		}
	});
})(Ccay.ViewLayout);